﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.WPF.Theme.Application
 * 唯一标识：5be8bfec-8651-422d-96af-f3b058175b8d
 * 文件名：VampirewalApplication
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2023/9/25 13:56:16
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

using Microsoft.Win32;
using System.Threading.Tasks;
using Vampirewal.Core.Interface;

namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// Vampirewal版本的Application
/// </summary>
public abstract class VampirewalApplication : Application
{
    /// <summary>
    /// 获取引导启动类型
    /// </summary>
    /// <returns></returns>
    protected abstract Type BootStartUp();

    ///// <summary>
    ///// 全局异常捕获(需重写)
    ///// <para>错误信息：e.Exception.Message</para>
    ///// <para>最后需要写e.Handled = true;证明异常已处理</para>
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="e"></param>
    //protected virtual void GlobalExceptions(object sender, DispatcherUnhandledExceptionEventArgs e)
    //{
    //    //IDialogMessage dialog = new VampirewalDialog();
    //    //dialog.ShowPopupWindow($"错误信息：{e.Exception.Message}",)
    //    e.Handled = true;
    //}



    /// <summary>
    /// 重写需保留base！！！
    /// </summary>
    /// <param name="e"></param>
    protected override void OnStartup(StartupEventArgs e)
    {
        if (BootStartUp() == null)
        {
            throw new Exception("未提供引导启动类！");
        }
        //else if (BootStartUp().BaseType != typeof(IVampirewalBootStartUp))
        //{
        //    throw new Exception("提供引导启动类不是派生自 VampirewalBootStartUp ！");
        //}
        else if (BootStartUp().GetInterfaces().FirstOrDefault(f => f == typeof(IVampirewalBootStartUp)) == null)
        {
            throw new Exception("提供引导启动类不是派生自 IVampirewalBootStartUp ！");
        }

        base.OnStartup(e);

        #region 将OpenFileDialog的Type注册进程序上下文中

        var DialogType = typeof(OpenFileDialog);

        VampirewalCoreContext.GetInstance().AddContext("OpenFileDialog", DialogType);

        #endregion

        var bootStartup = (IVampirewalBootStartUp)Activator.CreateInstance(BootStartUp());

        var OnAppRun = BootStartUp().GetMethod("OnAppRun");
        var OnAppExit = BootStartUp().GetMethod("OnAppExit");
        var GlobalException = BootStartUp().GetMethod("GlobalException");

        bootStartup.BuildBootStartUp(e.Args);

        #region 全局异常捕获
        AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
        {
            GlobalException?.Invoke(bootStartup, new object[2] { "UnhandledException(当某个异常未被捕获时出现)", e.ExceptionObject as Exception });
        };

        DispatcherUnhandledException += (sender, e) =>
        {
            GlobalException?.Invoke(bootStartup, new object[2] { "DispatcherUnhandledException(在异常由应用程序引发但未进行处理时发生)", e.Exception });

            e.Handled = true;
        };
        TaskScheduler.UnobservedTaskException += (sender, e) =>
        {
            GlobalException?.Invoke(bootStartup, new object[2] { "UnobservedTaskException(出错的任务中未观察到的异常将触发异常呈报策略时出现，默认情况下会终止进程)", e.Exception });
            e.SetObserved();
        };
        #endregion

        OnAppRun?.Invoke(bootStartup, new object[1] { true });

        Exit += (sender, e) =>
        {
            OnAppExit?.Invoke(bootStartup, new object[1] { false });
        };
    }
}

/// <summary>
/// 
/// </summary>
[VampirewalIoCRegister(typeof(IApplicationService))]
public class ApplicationService : IApplicationService
{
    /// <inheritdoc/>
    public void BeginInvoke(Action act)
    {
        Application.Current.Dispatcher.BeginInvoke(act);
    }
    /// <inheritdoc/>
    public void Shutdown()
    {
        Application.Current.Shutdown();
    }

}